home *** CD-ROM | disk | FTP | other *** search
- Path: newsstand.cit.cornell.edu!usenet
- From: Christopher Kline <ckline@tc.cornell.edu>
- Newsgroups: comp.lang.c++
- Subject: Problem with the relationship between friend and derived classes
- Date: Sun, 31 Mar 1996 16:19:04 -0500
- Organization: Cornell University
- Sender: cjk2@cornell.edu (Verified)
- Message-ID: <315EF6C8.41C6@tc.cornell.edu>
- NNTP-Posting-Host: crystal.tc.cornell.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; IRIX 5.3 IP22)
-
- I have a problem involving the relationship between friend classes and
- derived classes.
-
- My group is currently writing a simulation framework involving moving
- vehicles, each of which have an associated set of dynamics which
- control their movement and their interaction with other vehicles in
- the simulation.
-
- The problem I am having can be seen in this minimal example (the
- errors are included as comments at point where they occur):
-
-
- class engine {
-
- public:
- engine(car *c) {
- attachedCar = c;
- };
-
- virtual void update(void) = 0;
-
- protected:
- car *attachedCar;
- };
-
- //--------------------
-
- class car {
-
- friend engine;
-
- public:
- car(void) {
- velocity = 0;
- }
-
- virtual void move(void) = 0;
-
- protected:
- engine *e;
- double velocity;
-
- };
-
- // -------------------
-
- class honda_engine : public engine {
-
- public:
- honda_engine(honda *h)
- : engine(h) { }
-
- void update(void) {
-
- // Error #1: "engine::attachedCar" is inaccessible
- // Error #2: "car::velocity" is inaccessible
- attachedCar->velocity++;
-
- // Error #3: "honda_miles" is undefined
- honda_miles++;
- }
-
- };
-
- //--------------------
-
- class honda : public car {
-
- friend honda_engine;
-
- public:
- honda(void) {
- honda_miles = 0;
- e = new honda_engine(this);
- }
-
- void move(void) {
- e->update();
- }
-
- private:
- int honda_miles;
-
- };
-
- //--------------------
-
-
- Engines are friends of their respective cars and the base classes for
- engines and cars are both abstract classes (hence only instances of
- the derived classes will be created).
-
- The problem is that I would like:
-
- 1) a derived engine to have access to the protected members of the
- derived car class of which it is a friend, and of the parent class(es)
- of that car.
-
- 2) a derived engine to access the correct class of car when it
- dereferences the attachedCar member (attachedCar will always be
- something derived from car, even if the compiler can't understand
- that).
-
-
- I understand WHY these three errors are occurring, but I can't figure
- out HOW to get my code to behave the way I want it to (i.e., using the
- relationship between cars/engines that I've set up).
-
- Does anyone have any clue on how to get this to work? It seems like it
- would be a fairly common design problem, but I haven't found reference
- to it in any of my books or the FAQ.
-
- Any help would be greatly appreciated. Also, if you would kindly CC:
- me on the reply, that would be great, in case I miss the post in the
- newsgroup.
-
- --
- Christopher Kline
- http://www.tc.cornell.edu/~ckline/
-
- to compose, Decompose, I wander and slip
-